package com.deepclassifieds.daas.api.jdbc.name;

import java.io.Serializable;

import com.deepclassifieds.daas.api.DaasUID;
import com.deepclassifieds.daas.api.xsd.Shard;
import com.deepclassifieds.daas.common.Name;
import com.deepclassifieds.daas.common.Reject;

public final class DatabaseName implements Serializable, Comparable<DatabaseName> {

	private static final long serialVersionUID = 20110222003342L;

	private static final String FORMAT = "%s_%0" + DaasUID.SHARD_NUMBER_MAX_DIGITS + "d";
	
	private final Shard shard;
	private final Name databaseName;
	private final String toString;
	
	public DatabaseName(Name databaseName, Shard shard) {
		Reject.ifNull(shard);
		Reject.ifNull(databaseName);
		this.shard = shard;
		this.databaseName = databaseName;
		this.toString = String.format(FORMAT, databaseName.toString(), shard.getNumber());		
	}

	public Shard getShard() {
		return shard;
	}

	public Name getDatabaseName() {
		return databaseName;
	}

	@Override
	public int hashCode() {
		return toString.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		DatabaseName other = (DatabaseName) obj;
		return toString.equals(other.toString);
	}

	@Override
	public String toString() {
		return toString;
	}

	@Override
	public int compareTo(DatabaseName value) {
		return toString.compareTo(value.toString);
	}
	
}
